package com.xszx.serviceedu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.commonutils.JwtUtils;
import com.xszx.commonutils.R;
import com.xszx.serviceedu.client.OrderClient;
import com.xszx.serviceedu.entity.Course;
import com.xszx.serviceedu.entity.form.CourseInfoForm;
import com.xszx.serviceedu.entity.query.CourseQuery;
import com.xszx.serviceedu.entity.vo.ChapterVo;
import com.xszx.serviceedu.entity.vo.CoursePublishVo;
import com.xszx.serviceedu.entity.vo.CourseQueryVo;
import com.xszx.serviceedu.entity.vo.CourseWebVo;
import com.xszx.serviceedu.service.ChapterService;
import com.xszx.serviceedu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 白熊
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/serviceedu/course")
//@CrossOrigin
@Api(tags = "课程管理")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("/font/{courseId}")
    public R getByIdForFont(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable("courseId") String courseId,
            HttpServletRequest request){
        //查询课程信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        //查询章节嵌套信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        //查询订单信息
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录后再进行购买");
        }

        boolean b = orderClient.isBuyCourse(memberId, courseId);

        return R.ok().data("course", courseWebVo)
                .data("chapterVoList", chapterVoList)
                .data("isbuy",b);
    }


    @ApiOperation(value = "前台的分页模糊查询课程列表")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<Course> pageParam = new Page<>(page,limit);
        //传入模糊查询的条件
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return R.ok().data(map);//返回给前台一个map

    }

    @DeleteMapping("{id}")
    @ApiOperation("根据课程ID删除课程")
    public R removeById(@ApiParam(name = "id",value = "课程ID",required = true)
                                    @PathVariable String id){
        courseService.removeCourseById(id);
        return R.ok().message("删除成功!");
    }

    /**
     * 用get那么前端传给后端的数据用params: 不用data
     * courseQuery 前端传过来类型是application/json
     * 因此需要用post/put 只有post前端能把数据传给后端
     * 当用了post/put的时候 就必须要用到 @RequestBody注解
     */
    @GetMapping("{page}/{limit}")
    @ApiOperation("分页模糊查询")
    public R pageQuery(@ApiParam(name = "page",value = "第几页",defaultValue = "1",required = true)
                                   @PathVariable Long page,
                       @ApiParam(name = "limit",value = "每页数据几条",defaultValue = "5",required = true)
                               @PathVariable Long limit,
                       CourseQuery courseQuery){
        Page<Course> pageParam = new Page<>(page,limit);
        courseService.pageQuery(pageParam,courseQuery);
        return R.ok().data("records", pageParam.getRecords())
                .data("total",pageParam.getTotal());
    }



    @PutMapping("/publishCourseById/{id}")
    @ApiOperation("发布课程")
    public R publishCourseById(@ApiParam(name = "id",value = "课程id",required = true)
                                           @PathVariable String id){
        courseService.publishCourseById(id);
        return R.ok().message("课程发布成功");
    }

    @GetMapping("/getCoursePublishVoById/{id}")
    @ApiOperation("课程信息展示")
    public R getCoursePublishVoById(@ApiParam(name = "id",value = "课程id",required = true)
                                                @PathVariable String id){
        CoursePublishVo publishVo = courseService.getCoursePublishVoById(id);
        return R.ok().data("item",publishVo);
    }

    @PutMapping("{id}")
    @ApiOperation("根据ID修改课程星系信息列表")
    public R updateCourseInfoById(@ApiParam(name = "id",value = "课程id",required = true)
                                  @PathVariable String id,
                                  @ApiParam(name = "courseInfoForm",value = "信息列表",required = true)
                                  @RequestBody CourseInfoForm courseInfoForm){
        courseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("课程信息保存成功").data("courseId",courseInfoForm.getId());
    }

    @ApiOperation("根据id查询课程详细信息列表")
    @GetMapping("{id}")
    public R getById(@PathVariable("id") @ApiParam(name = "id",value = "课程id",required = true)
                                 String id){
        CourseInfoForm courseInfoFormById = courseService.getCourseInfoFormById(id);
        return R.ok().data("item",courseInfoFormById);

    }
    @PostMapping
    @ApiOperation("课程添加")
    public R addcourse(@ApiParam(name = "courseInfoForm", value = "课程信息对象", required = true)
                           @RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId);
    }
}

